package com.cloud.basic.test;

import java.util.*;

/**
 * @program: basic
 * @description: list中map按照某个value排序
 * @author: Cloud
 * @create: 2021/7/8 10:29:24
 */
public class testListMapSort {

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        map.put("code","code01");
        map.put("yield",1);
        list.add(map);
        System.out.println("map.get(\"code\") = " + map.get("code").hashCode());
        // 这里map已经变成了一个新的对象，
        map = new HashMap<>();
        map.put("code","code03");
        map.put("yield",3);
        System.out.println("map.get(\"code\") = " + map.get("code").hashCode());
        list.add(map);
        map = new HashMap<>();
        map.put("code","code02");
        map.put("yield",2);
        System.out.println("map.get(\"code\") = " + map.get("code").hashCode());
        list.add(map);
        for (Map<String, Object> map1 : list) {
            System.out.println("map1.get(\"code\") = " + map1.get("code"));
            System.out.println("map1.get(\"yield\") = " + map1.get("yield"));
        }
        Collections.sort(list, new Comparator<Map<String,Object>>(){
            public int compare(Map<String,Object> o1,Map<String,Object> o2){
                return (Integer)o1.get("yield")<(Integer) o2.get("yield")?1:(Integer)o1.get("yield")==(Integer)o2.get("yield")?0:-1;
            }
        });
        System.out.println(list);
    }
}
